package cn.niit.letuseatdemo.controller.user;

import cn.niit.letuseatdemo.common.enums.ExceptionCodeEnum;
import cn.niit.letuseatdemo.entity.domain.Goods;
import cn.niit.letuseatdemo.entity.domain.Store;
import cn.niit.letuseatdemo.entity.domain.User;
import cn.niit.letuseatdemo.entity.domain.UserAddress;
import cn.niit.letuseatdemo.entity.vo.response.BaseResponse;
import cn.niit.letuseatdemo.service.UserAddressService;
import cn.niit.letuseatdemo.service.UserService;
import cn.niit.letuseatdemo.util.H5LoginUtil;
import cn.niit.letuseatdemo.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *用户信息更新
 * @Author: JinNa ToDo
 * @Date: 2022/11/01/23:42
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private UserAddressService userAddressService;


    /*
    * @Description:获取用户个人信息接口
    * @Author: Jin_Na
    * @CreateTime: 2022/12/8 17:00
     * @param: token
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
    **/
    @GetMapping
    public BaseResponse user(@RequestHeader("auth-token") String token){
        log.info("获取个人信息接口：" + token);
        //根据token获取真实的用户信息
        User user = (User) redisUtil.get("token-" + token);
        return BaseResponse.success(user);
    }

    /**
    * @Description: 通过公共的用户登录工具类获取登录用户信息
    * @Author: Jin_Na
    * @CreateTime: 2022/12/8 17:01
     * @param: user
     * @return: java.lang.String
    **/
    @GetMapping("/user")
    public BaseResponse getUserInfo(){
        User user = H5LoginUtil.getLoginUser();//通过公共的用户登录工具类获取登录用户信息
        log.info("【H5LoginUtil】获取个人信息接口：user = {}",user);
        return  BaseResponse.success(user);
    }

    /*
    * @Description: 删除用户信息
    * @Author: Jin_Na
    * @CreateTime: 2022/12/8 19:10
     * @param: id
     * @param: servletResponse
     * @return: java.lang.String
    **/
    @DeleteMapping
    @ResponseBody
    public String delete(@PathVariable Long id, HttpServletResponse servletResponse) throws IOException{
        Integer count = userService.deleteUserByUserId(id);
        if (count==1){
            servletResponse.sendRedirect("/list");
        }
        return "error";
    }

    /*
    * @Description: 更新并保存
    * @Author: Jin_Na
    * @CreateTime: 2022/12/8 19:10
     * @param: user
     * @return: java.lang.String
    **/
    @PutMapping
    public BaseResponse editSave(@RequestBody User user)
    {
        int rows = userService.updateUser(user);
        if (rows == 1){
            return BaseResponse.success();
        }
        return BaseResponse.error(ExceptionCodeEnum.COMMON_EDIT_ERROR);
    }


    /*
     * @Description: 根据userid查地址
     * @Author: Zhang Shiqi
     * @CreateTime： 2023/4/18 17:03
     * @param: userId
     * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
     **/
    @GetMapping("/address/{userId}")
    public BaseResponse addressMes(UserAddress userAddress, @PathVariable("userId")Long userId){
        List<UserAddress> userAddresses =userAddressService.selectUserAddressList(userAddress,userId);
        Map<String, Object> map = new HashMap<>();
        map.put("userAddresses", userAddresses);
        return BaseResponse.success(map);
    }

}